<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="filters.name" placeholder="名称" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />
      <el-input v-model="filters.author" placeholder="作者" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />
      <el-input v-model="filters.tag" placeholder="标签" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />
      <movie-level-select v-model="filters.movieLevelId" />
      <br>玩家数量
      <el-input-number v-model="filters.minPlayers" :min="1" controls-position="right" class="filter-item" @keyup.enter.native="handleFilter" />
      - <el-input-number v-model="filters.maxPlayers" :min="filters.minPlayers" controls-position="right" class="filter-item" @keyup.enter.native="handleFilter" />
      &nbsp;&nbsp;时长
      <el-input-number v-model="filters.minDuration" :min="1" controls-position="right" class="filter-item" @keyup.enter.native="handleFilter" />
      - <el-input-number v-model="filters.maxDuration" :min="filters.minDuration" controls-position="right" class="filter-item" @keyup.enter.native="handleFilter" />小时
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">查询</el-button>
      <div style="margin-left: 3.125rem;">
        <el-checkbox v-for="col in dynamicColumn.movies" v-model="col.show" class="filter-item" style="margin-left:10px;" @change="tableKey=tableKey+1">
          {{ col.name }}
        </el-checkbox>
      </div>
    </div>

    <el-table
      :key="tableKey"
      v-loading="listLoading"
      border
      fit
      :height="tableHeight"
      :data="list"
      highlight-current-row
      style="width: 100%;"
      @sort-change="sortChange"
      @selection-change="selsChange"
    >
      <el-table-column type="index" width="60" />
      <el-table-column prop="name" label="名称" min-width="160" sortable />
      <el-table-column prop="author" label="作者" width="110" sortable  />
      <el-table-column prop="version" label="版本" width="100" sortable  />
      <el-table-column v-if="checkColumn('tag')" prop="tag" label="标签" min-width="160" :formatter="formatTag" />
      <el-table-column v-if="checkColumn('duration')" prop="duration" label="时长(小时)" width="90" />
      <el-table-column v-if="checkColumn('publishTime')" prop="publishTime" label="发布时间" width="160" sortable/>
      <el-table-column label="玩家数量" width="100" :formatter="formatPlayers" />
      <el-table-column v-if="checkColumn('playTimes')" prop="playTimes" label="开本次数" width="110" sortable />
      <el-table-column prop="price" label="价格(元)" width="80" :formatter="formatPrice" />
      <el-table-column prop="price" label="开放价格(元)" width="110" :formatter="formatOpenPrice" />
      <el-table-column prop="status" label="状态" width="70" :formatter="formatStatus" />
      <el-table-column v-if="checkColumn('movieLevelName')" prop="movieLevelName" label="类别" width="90" />
      <el-table-column v-if="checkColumn('operation')" label="操作" width="180" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button v-if="scope.row.status === 1" size="small" @click="online(scope.index, scope.row)">上线</el-button>
          <el-button v-if="scope.row.status === 0" size="small" @click="offline(scope.index, scope.row)">下线</el-button>
<!--          <el-button size="small" @click="handlePrice(scope.index, scope.row)">修改价格</el-button>-->
          <el-button size="small" @click="handleLevel(scope.index, scope.row)">修改类别</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="filters.page" :limit.sync="filters.limit" @pagination="loadPageList" />

    <!--修改价格-->
    <el-dialog title="修改价格" :visible.sync="priceFormVisible" :close-on-click-modal="false">
      <el-form ref="priceForm" :model="priceForm" :label-width="formLabelWidth" :rules="priceFormRules">
        <el-form-item label="价格" prop="price">
          <el-input-number v-model="priceForm.price" :min="0" controls-position="right" @keyup.enter.native="priceSubmit" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="priceFormVisible = false">取消</el-button>
        <el-button type="primary" :loading="priceFormLoading" @click="priceSubmit">提交</el-button>
      </div>
    </el-dialog>

    <!--修改价格-->
    <el-dialog title="修改类别" :visible.sync="levelFormVisible" :close-on-click-modal="false">
      <el-form ref="levelForm" :model="levelForm" :label-width="formLabelWidth" :rules="levelFormRules">
        <el-form-item label="类别" prop="level">
          <movie-level-select v-model="levelForm.movieLevelId"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="levelFormVisible = false">取消</el-button>
        <el-button type="primary" :loading="levelFormLoading" @click="levelSubmit">提交</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import waves from '@/directive/waves' // waves directive
import { mapGetters } from 'vuex'
import Pagination from '@/components/Pagination'
import { listMovie, online, offline, setPrice, setLevel } from '@/api/movie'
import MovieLevelSelect from '@/views/components/movie-level-select'
import util from '@/utils/util'

export default {
  name: 'MovieList',
  components: { MovieLevelSelect, Pagination },
  directives: { waves },
  data() {
    return {
      tableHeight: 260,
      tableKey: 0,
      list: null,
      total: 0,
      listLoading: true,
      sels: [], // 列表选中列
      filters: {
        page: 1,
        limit: 20,
        name: '',
        author: null,
        tag: null,
        movieLevelId: null,
        minPlayers: 0,
        maxPlayers: 0,
        minDuration: 0,
        maxDuration: 0
      },
      orderBy: {
        orderColumn: '',
        order: ''
      },
      editId: 0,
      priceFormVisible: false,
      priceFormLoading: false,
      priceForm: {
        price: 100
      },
      priceFormRules: {
        price: [{ required: true, message: '请输入价格', trigger: 'blur' }]
      },
      levelFormVisible: false,
      levelFormLoading: false,
      levelForm: {
        movieLevelId: 100
      },
      levelFormRules: {
        movieLevelId: [{ required: true, message: '请选择类别', trigger: 'blur' }]
      }
    }
  },
  computed: {
    ...mapGetters(['userInfo', 'formLabelWidth', 'dynamicColumn'])
  },
  created() {
    this.tableHeight = window.innerHeight - 260
    this.loadPageList()
  },
  methods: {
    checkColumn(col) {
      if (this.dynamicColumn.movies.hasOwnProperty(col)) {
        return this.dynamicColumn.movies[col].show
      }
      return true
    },
    formatTag(row) {
      if (row.tag && Array.isArray(row.tag)) {
        return row.tag.join(' ')
      }
    },
    formatPlayers(row) {
      if (row.minPlayers && row.maxPlayers) {
        return row.minPlayers + '-' + row.maxPlayers + '人'
      } else if (row.minPlayers) {
        return '至少' + row.minPlayers + '人'
      } else if (row.maxPlayers) {
        return '最多' + row.maxPlayers + '人'
      } else {
        return ''
      }
    },
    formatPrice(row) {
      return util.currency.format(row.price)
    },
    formatOpenPrice(row) {
      return util.currency.format(row.openPrice)
    },
    formatStatus(row) {
      const st = parseInt(row.status)
      if (st === 0) {
        return '已上线'
      } else if (st === 1) {
        return '已下线'
      }
      return ''
    },
    loadPageList() {
      this.listLoading = true
      const param = { ...this.filters, ...this.orderBy }
      listMovie(param).then(({ success, message, data }) => {
        this.listLoading = false
        if (success) {
          const { records, total } = data || {}
          this.list = records
          this.total = total
        } else {
          this.$message({
            message: message || '查询列表失败',
            type: 'error'
          })
        }
      }, () => {
        this.listLoading = false
      })
    },
    handleFilter() {
      this.filters.page = 1
      this.loadPageList()
    },
    sortChange({ prop, order }) {
      this.orderBy.orderColumn = prop
      this.orderBy.order = order
      this.handleFilter()
    },
    selsChange(sels) {
      this.sels = sels
    },
    online(index, row) {
      this.$confirm('确认上线吗？', '提示', {}).then(() => {
        this.listLoading = true
        online(row.id).then(({ success, message }) => {
          this.listLoading = false
          if (success) {
            this.loadPageList()
          } else {
            this.$message({
              message: message || '操作失败',
              type: 'error'
            })
          }
        }, () => {
          this.listLoading = false
        })
      })
    },
    offline(index, row) {
      this.$confirm('确认下线吗？', '提示', {}).then(() => {
        this.listLoading = true
        offline(row.id).then(({ success, message }) => {
          this.listLoading = false
          if (success) {
            this.loadPageList()
          } else {
            this.$message({
              message: message || '操作失败',
              type: 'error'
            })
          }
        }, () => {
          this.listLoading = false
        })
      })
    },
    handlePrice(index, row) {
      this.priceFormVisible = true
      this.editId = row.id
      this.priceForm.price = row.price
      this.$nextTick(() => {
        this.$refs['priceForm'].clearValidate()
      })
    },
    priceSubmit() {
      this.$confirm('确认提交吗？', '提示', {}).then(() => {
        this.priceFormLoading = true
        setPrice(this.editId, this.priceForm).then(({ success, message }) => {
          this.priceFormLoading = false
          if (success) {
            this.$message({
              message: message || '提交成功',
              type: 'success'
            })
            this.$refs['priceForm'].resetFields()
            this.priceFormVisible = false
            this.loadPageList()
          } else {
            this.$message({
              message: message || '提交失败',
              type: 'error'
            })
          }
        }, () => {
          this.priceFormLoading = false
        })
      })
    },
    handleLevel(index, row) {
      this.levelFormVisible = true
      this.editId = row.id
      this.levelForm.movieLevelId = row.movieLevelId
      this.$nextTick(() => {
        this.$refs['levelForm'].clearValidate()
      })
    },
    levelSubmit() {
      this.$confirm('确认提交吗？', '提示', {}).then(() => {
        this.levelFormLoading = true
        setLevel(this.editId, this.levelForm.movieLevelId).then(({ success, message }) => {
          this.levelFormLoading = false
          if (success) {
            this.$message({
              message: message || '提交成功',
              type: 'success'
            })
            this.$refs['levelForm'].resetFields()
            this.levelFormVisible = false
            this.loadPageList()
          } else {
            this.$message({
              message: message || '提交失败',
              type: 'error'
            })
          }
        }, () => {
          this.levelFormLoading = false
        })
      })
    }
  }
}
</script>

<style scoped>

</style>
